package com.neusoft.mapper;

import com.neusoft.core.Food;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FoodMapper {

	// 根据商家ID列出所有食物
	@Select("SELECT * FROM food WHERE businessId = #{businessId} ORDER BY foodId")
	List<Food> listFoodByBusinessId(@Param("businessId") Integer businessId);

	// 根据食物ID查询食物
	@Select("SELECT * FROM food WHERE foodId = #{foodId}")
	Food getFoodById(@Param("foodId") Integer foodId);

	// 更新库存（下单时减少库存）
	@Update("UPDATE food SET quantity = quantity - #{odQuantity} WHERE foodId = #{foodId}")
	int orderFood(@Param("foodId") Integer foodId, @Param("odQuantity") Integer odQuantity);

	// 添加新食物
	@Insert("INSERT INTO food (foodName, foodExplain, foodImg, foodPrice, businessId, quantity) " +
			"VALUES (#{foodName}, #{foodExplain}, #{foodImg}, #{foodPrice}, #{businessId}, #{quantity})")
	int saveFood(@Param("foodName") String foodName,
				 @Param("foodExplain") String foodExplain,
				 @Param("foodImg") String foodImg,
				 @Param("foodPrice") Double foodPrice,
				 @Param("businessId") Integer businessId,
				 @Param("quantity") Integer quantity);

	// 将食物从商家中移除
	@Update("UPDATE food SET businessId = 0 WHERE foodId = #{foodId}")
	int removeFood(@Param("foodId") Integer foodId);

	// 更新食物信息
	@Update("UPDATE food SET foodName = #{foodName}, foodExplain = #{foodExplain}, " +
			"foodImg = #{foodImg}, foodPrice = #{foodPrice}, businessId = #{businessId}, quantity = #{quantity} " +
			"WHERE foodId = #{foodId}")
	int updateFood(Food food);

	// 删除购物车中相关的食物记录
	@Delete("DELETE FROM cart WHERE foodId = #{foodId}")
	int deleteCart(@Param("foodId") Integer foodId);

	// 删除未支付订单中包含该食物的订单
	@Delete("DELETE FROM orders WHERE orderId IN " +
			"(SELECT orderId FROM orderDetailet WHERE foodId = #{foodId}) " +
			"AND orderState = 0")
	int deleteOrderByFoodId(@Param("foodId") Integer foodId);
}
